#if !defined( __MEMCACHEASIOPRIVATE_H__)
#define __MEMCACHEASIOPRIVATE_H__

/* Copyright (c) 2008, dZettaApart.
   Licensed under the BSD License.
   See COPYING for details. */

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>

#include <deque>
#include <list>
#include <iostream>

#include "memcacheasio_request.h"
#include "memcacheasio_connection.h"

namespace memcacheasio
{
	class ConnectionsPool;

	class Memcacheasio_p : public boost::enable_shared_from_this<Memcacheasio_p>, private boost::noncopyable
	{
		public:
			typedef boost::shared_ptr< Memcacheasio_p > Ptr;

		public:
			Memcacheasio_p();
			~Memcacheasio_p();

			void init ( const std::string& /*host*/, const std::string& /*port*/, int count_connection = 1, int count_thread = 1, int tryCount = 1, int tryTimeout = 20, int limitConnection = 1 );

			void stop();

			void execute ( Request::Ptr /*request*/, boost::function1<void, Request::Ptr > /*cb*/ );

		protected:
			/// Run the server's io_service loop.
			std::size_t run();

			void processRequest( Request::Ptr /*request*/, boost::function1<void, Request::Ptr > /*cb*/, Connection::Ptr /*connection*/, const boost::system::error_code& /*err*/ );
			void completeRequest ( boost::function1<void, Request::Ptr > cb, const boost::system::error_code& /*err*/, Request::Ptr /*request*/ );

		private:
			boost::shared_ptr<ConnectionsPool>	_pool;

			boost::asio::io_service  			_io_service;
			boost::scoped_ptr<boost::asio::io_service::work> _work;

			std::vector< boost::shared_ptr<boost::thread> >  _threadList;

			boost::mutex _mutex;
	};

} //namespace memcacheasio

#endif // !defined(__MEMCACHEASIO_H__)
